package jumtsmon;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class umtsWriter implements Runnable {
    public static String serialDevice;
           static BufferedWriter out;
           static int  count;
    public static String command;

    umtsWriter() throws IOException {
        serialDevice = Main.SerialPort;
        count = 0;
        command = "atz";
        try {
            out = new BufferedWriter(new FileWriter(serialDevice));
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public void run() {
        try {
            sendToModem(command);
            Thread.sleep(10000);
        }
       
        catch (InterruptedException ex) {
                Logger.getLogger(umtsWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        command="at+cops?";
        while(true) {
          try {
               sendToModem(command);
               roaming.getConnectedOp();
               Thread.sleep(10000);

          }
           catch (NullPointerException e) {
                System.out.println("------ > eccezione ");
          }
          catch (InterruptedException ex) {
                Logger.getLogger(umtsWriter.class.getName()).log(Level.SEVERE, null, ex);
          }
       }
    }

    private static void writeToFile(String command) {
        try {
            System.out.println("Sending : " + command + " count " + count);
            out.write(command);
            out.flush();
        }
        catch (IOException e) {
        }
    }

    private static void sendToModem(String command) {
        count++;
        String send = command.toUpperCase() + "\r";
        writeToFile(send);
    }
}
